'use strict';

angular.module('afasApp')
  .controller('DiscountsCtrl', ['$scope', '$state', 'Services', '_', function ($scope, $state, Services, _) {

    $scope.discounts = [];
    $scope.discNew = {};

    $scope.amountTypes = [
      {
        id: 2,
        label: 'Nominal (.00)'
      },
      {
        id: 5,
        label: 'Percentage (%)'
      },
    ];

    $scope.TransAmountType = function(id) {
      console.log(_.where($scope.amountTypes, {id: id}));
      return _.where($scope.amountTypes, {id: id})[0].label;
    };

    function applyRemoteData( newData ) {
      $scope.discounts = newData;
    }

    function loadRemoteData() {
      Services.getItems('discounts')
      .then(function( results ) {
        applyRemoteData( results.data );
      });
    }

    loadRemoteData();

    $scope.addDisc = function () {
      Services.add('discounts', $scope.discNew)
      .then(function(results){
        console.log(results);
        $scope.discNew = {};
        $state.go( '^.list' );
      }, function(results){
        if (results.status == 400) {
          $scope.errors = results.data;
        }
      });
    };

    $scope.deleteDisc = function (disc) {
      Services.delete('discounts', disc)
      .then(function(results){
        console.log(results);
        loadRemoteData();
      });
    };
  }]).controller('DiscountCtrl', ['$scope', 'Services', '$state', '$stateParams', function ($scope, Services, $state, $stateParams) {

    $scope.amountTypes = [
      {
        id: 2,
        label: 'Nominal (.00)'
      },
      {
        id: 5,
        label: 'Percentage (%)'
      },
    ];

    $scope.currentDisc = {};
    $scope.currentID = $stateParams.id;
    $scope.currentDisc = {};

    Services.getItem('discounts', $stateParams.id)
    .then(function(results){
      $scope.currentDisc = results.data;
    });

    $scope.updateDisc = function () {
      Services.update('discounts', $scope.currentDisc, $scope.currentID);
      $state.go( 'discounts.show', {id: $scope.currentID} );
    };
  }]);